package cn.edu.ldu.communityqa.communityqa.dao;

import cn.edu.ldu.communityqa.communityqa.entity.Question;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@Repository
public class QuestionDao {

    private final JdbcTemplate jdbcTemplate;

    public QuestionDao(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    // 保存新问题
    public int save(Question question) {
        String sql = "INSERT INTO question (user_id, title, content) VALUES (?, ?, ?)";
        return jdbcTemplate.update(
            sql,
            question.getUserId(),
            question.getTitle(),
            question.getContent()
        );
    }

    // 更新问题
    public int update(Question question) {
        String sql = "UPDATE question SET title = ?, content = ? WHERE id = ?";
        return jdbcTemplate.update(
            sql,
            question.getTitle(),
            question.getContent(),
            question.getId()
        );
    }

    // 根据 ID 删除问题
    public int deleteById(Long id) {
        String sql = "DELETE FROM question WHERE id = ?";
        return jdbcTemplate.update(sql, id);
    }

    // 根据 ID 查询问题
    public Question findById(Long id) {
        String sql = "SELECT * FROM question WHERE id = ?";
        return jdbcTemplate.queryForObject(sql, new QuestionRowMapper(), id);
    }

    // 根据用户 ID 查询问题
    public List<Question> findByUserId(Long userId) {
        String sql = "SELECT * FROM question WHERE user_id = ?";
        return jdbcTemplate.query(sql, new QuestionRowMapper(), userId);
    }

    // 查询所有问题
    public List<Question> findAll() {
        String sql = "SELECT * FROM question";
        return jdbcTemplate.query(sql, new QuestionRowMapper());
    }

    // 分页查询
    public List<Question> findAllWithPagination(int offset, int limit) {
        String sql = "SELECT * FROM question LIMIT ? OFFSET ?";
        return jdbcTemplate.query(sql, new QuestionRowMapper(), limit, offset);
    }

    // 根据标题模糊查询
    public List<Question> findByTitleLike(String title) {
        String sql = "SELECT * FROM question WHERE title LIKE ?";
        return jdbcTemplate.query(sql, new QuestionRowMapper(), "%" + title + "%");
    }

    // 内部类：QuestionRowMapper，将 ResultSet 映射到 Question 对象
    static class QuestionRowMapper implements RowMapper<Question> {
        @Override
        public Question mapRow(ResultSet rs, int rowNum) throws SQLException {
            Question question = new Question();
            question.setId(rs.getLong("id"));
            question.setUserId(rs.getLong("user_id"));
            question.setTitle(rs.getString("title"));
            question.setContent(rs.getString("content"));
            question.setCreatedAt(rs.getTimestamp("created_at").toLocalDateTime());
            question.setUpdatedAt(rs.getTimestamp("updated_at").toLocalDateTime());
            return question;
        }
    }
}